<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        var minMoves = function (nums, k) {
            // let sum = 0
            // for (let i = 0; i < nums.length; i++){
            //     sum += nums[i]
            // }
            // while(k>0){
            //     let min = Math.min(...nums)
            //     let index = nums.indexOf(min)
            //     nums[index] = -min
            //     sum = sum - min*2
            //     k--
            // }
            // return sum
            let sum = 0
            nums.sort((a,b)=>Math.abs(b)-Math.abs(a))
            for (let i = 0; i < nums.length; i++){
                if (nums[i]<0&&k>0){
                    nums[i] = -nums[i]
                    k--
                }
            }
            if (k%2===1){
                nums[nums.length-1] *= -1
            }
            for (let i = 0; i < nums.length; i++){
                sum += nums[i]
            }
            return sum
        }
        console.log(minMoves([1,2,3,4,5],2))
    </script>
</body>
</html>